package com.example.zhish.viewflipper_demo001;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {
    private ViewFlipper viewFlipper;
    private int[] resID ={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
    private float startx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
        //动态导入的方式为ViewFlipper加载子View
        for(int i =0;i<resID.length;i++){
            viewFlipper.addView(getImageView(resID[i]));

        }

        //添加动画效果
        viewFlipper.setInAnimation(this,R.anim.left_in);
        viewFlipper.setOutAnimation(this,R.anim.left_out);
        //设置视图切换的时间间隔
        viewFlipper.setFlipInterval(3000);
        //开始播放
        //viewFlipper.startFlipping();
    }

    //监控手势的函数
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                startx = event.getX();

                break;
            case MotionEvent.ACTION_MOVE:
                if(event.getX()-startx>100){

                    viewFlipper.setInAnimation(this,R.anim.left_in);
                    viewFlipper.setOutAnimation(this,R.anim.left_out);
                    viewFlipper.showPrevious();
                }else if(startx - event.getX()>100){
                    viewFlipper.setInAnimation(this,R.anim.right_in);
                    viewFlipper.setOutAnimation(this,R.anim.right_out);
                    viewFlipper.showNext();
                }
                break;
            case MotionEvent.ACTION_UP:

                break;
        }


        return super.onTouchEvent(event);
    }

    private ImageView getImageView(int resID){
        ImageView imageView = new ImageView(this);
        //imageView.setImageResource(resID);
        //设置成背景会全屏显示
        imageView.setBackgroundResource(resID);
        return imageView;
    }
}
